import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import nprogress from 'nprogress';
import { pendingRequest } from '@/utils/request';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Index',
    component: () => import('@/views/index.vue')
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  nprogress.start();
  next();
  // 取消上个页面未完成的请求
  pendingRequest.forEach((item) => item.routeChangeCancel && item.cancel());
  nprogress.done();
});

export default router
